/*
 * Copyright (C) 2011 Chris Gao <chris@exina.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dwt.widget;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.widget.TextView;

public class DayCell {
	
	private static final String TAG = "DayCell";
	public static final int PREVIOUS_MONTH = -1;
	public static final int CURRENT_MONTH = 0;
	public static final int NEXT_MONTH = 1;
	
	private Drawable mBackground;
	private Rect mBound = null;
	private int mDayOfMonth = 1;		// from 1 to 31
	private Paint mPaint = new Paint(Paint.SUBPIXEL_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
	private int dx, dy;
	
	private int mFlag;		// last month, this month and next month
	private boolean mHasEvent; 	// if there are events or not
	private int mNumOfEvent;		// number of events
	
	
	
	public DayCell(int dayOfMonth, int flag) {
		
		mDayOfMonth = dayOfMonth;
		mFlag = flag;
	}
	
	public DayCell(int dayOfMonth, Rect rect, float textSize, boolean bold) {
		
		mDayOfMonth = dayOfMonth;
		mBound = rect;
		
		mPaint.setTextSize(textSize);
		mPaint.setColor(Color.BLACK);
		
		if (bold) {
			
			mPaint.setFakeBoldText(true);
		}
		
		dx = (int) mPaint.measureText(String.valueOf(mDayOfMonth)) / 2;
		dy = (int) (-mPaint.ascent() + mPaint.descent()) / 2;
	}
	
	public DayCell(int dayOfMon, Rect rect, float textSize) {
		
		this(dayOfMon, rect, textSize, false);
	}
	
	public int getFlag() {
		
		return mFlag;
	}
	
	public boolean hasEvent() {
		
		return mHasEvent;
	}
	
	public void setEvent(boolean hasEvent) {
		
		mHasEvent = hasEvent;
	}
	
	public int getNumOfEvent() {
		
		return mNumOfEvent;
	}
	
	public void setNumOfEvent(int num) {
		
		mNumOfEvent = num;
		
		if (mNumOfEvent == 0) {
			
			mHasEvent = false;
			
		} else {
			
			mHasEvent = true;
		}
		
		return;
	}
	
	protected void draw(Canvas canvas) {
		
		canvas.drawText(String.valueOf(mDayOfMonth),
				mBound.centerX() - dx,
				mBound.centerY() + dy,
				mPaint);
	}
	
	public void setBackground(Drawable background) {
		
		mBackground = background;
	}
	
	public int getDayOfMonth() {
		
		return mDayOfMonth;
	}
	
	public boolean hitTest(int x, int y) {
		
		return mBound.contains(x, y); 
	}
	
	public Rect getBound() {
		
		return mBound;
	}
	
	public String toString() {
		
		return String.valueOf(mDayOfMonth);
	}
	
}

